﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using System.Windows.Forms;
using Microsoft.Office.Interop.Word;
using System.Xml.Linq;
using vmlModel;
using vmlModel.Entities;
using System.Windows.Markup;
using System.IO;

namespace WPFSLVstoDesigner
{
    public partial class WPFSLVstoDesignerRibbon : OfficeRibbon
    {
        public WPFSLVstoDesignerRibbon()
        {
            InitializeComponent();
        }

        private void WPFSLVstoDesignerRibbon_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void button1_Click(object sender, RibbonControlEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;

            string wordOpenXML = Globals.ThisAddIn.Application.ActiveDocument.WordOpenXML;

            XDocument xDoc = XDocument.Parse(wordOpenXML);

            var pakg = from mdoc in xDoc.Root.Descendants(vmlUtils.pkg + "part")
                       where mdoc.Attribute(vmlUtils.pkg + "name").Value == "/word/document.xml"     
                       select mdoc;

            var docbody = (from body in pakg.Descendants(vmlUtils.pkg + "xmlData")
                              .Descendants(vmlUtils.w + "document")
                              .Descendants(vmlUtils.w + "body")
                          select body).FirstOrDefault();

            OoxmlDoc oxmDoc = new OoxmlDoc(docbody);

            DesignerManager mngr = new DesignerManager();
            mngr.MainGrid.Children.Add(new ConverterMngr().GenerateXAMLDoc(oxmDoc));

            //mngr.vmlViewer.xmlContent = XamlWriter.Save(mngr.MainGrid); 
            mngr.Show();
            mngr.WriteXAML();
            MessageBox.Show(XamlWriter.Save(mngr.MainGrid)); 

        }
    }
}



//StringBuilder xx = new StringBuilder();
//foreach (var item in shapes)
//{
//    foreach (var atr in item.Attributes() )
//    {
//        xx.AppendLine(atr.Name + " == " + atr.Value);
//    }
//}
//MessageBox.Show(xx.ToString());